﻿/****************************** Module Header ******************************\
* Plus Name:  CatalogComboBox.js
* Plus Version:  1.0.0 (2012-1-10)
* Copyright (c) JianDanMai.Com (14507247@qq.com)
* 
* This plus used to option the catalog combobox webcontrols.
* 
* This source is subject to the GPL License.
* See http://www.gnu.org/licenses/gpl.html.
* All other rights reserved.
* 
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

/*
* ==Arguments:==
*   arrSelectIDs       :   分类select控件ID的数组
*   arrValues          :   分类初始值的数组
*   arrHiddenIDs       :   ASP.NET HiddenField服务器控件的编号的数组
*   url                :   AJAX动态获取数据的地址,生成的数据格式必须包含option标签
*   varname            :   父类别变量名称
* ==Demo:==
*   SetCatalogComboBox(["#s1", "#s2", "#s3"], 0, ["#h1", "#h2", "#h3"], '/AjaxCall/GetGoodSortList.aspx', 'pid');
*   SetCatalogComboBox(["#s1", "#s2", "#s3"], [7,121,662], ["#h1", "#h2", "#h3"], '/AjaxCall/GetGoodSortList.aspx', 'pid');
*   <select id="s1"></select>
*   <select id="s2"></select>
*   <select id="s3"></select>
*   <asp:HiddenField runat="server" ID="h1" />
*   <asp:HiddenField runat="server" ID="h2" />
*   <asp:HiddenField runat="server" ID="h3" />
*/
SetCatalogComboBox = function(arrSelectIDs, arrValues, arrHiddenIDs, url, varname) {
    $.ajax({ cache: false });
    if (varname == undefined) var varname = "";
    if (url == undefined) var url = "";
    if (varname) url += ((url.indexOf('?') > -1) ? '&' : '?') + varname + '=';
    var cb = {};
    cb.opt = '<option>请选择分类</option>';
    cb.sel = {};
    cb.indexOf = function(arr, el) {
        for (var i = 0; i < arr.length; i++) {
            if (arr[i] == el) return i;
        }
    }
    cb.pick = function(o, v) {
        o.children("option[value='" + v + "']").attr("selected", "selected");
    }
    for (var i = 0; i < arrSelectIDs.length; i++) {
        cb.c = i;
        if (arrValues) {
            cb.pid = i > 0 ? arrValues[i - 1] : 0;
            $(arrHiddenIDs[i]).val(arrValues[i]);
        } else {
            cb.pid = i > 0 ? ($(arrSelectIDs[i - 1]).val()) : 0;
        }
        cb.sel = $(arrSelectIDs[i]);
        if (isNaN(cb.pid)) {
            cb.sel.html(cb.opt);
        } else {
            cb.sel.load(url + cb.pid, function() {
                if (!arrValues) return;
                cb.$t = $(this);
                cb.pick(cb.$t, arrValues[cb.indexOf(arrSelectIDs, '#' + cb.$t.attr("id"))]);
            });
        }
        cb.sel.change(function() {
            cb.$t = $(this);
            cb.val = cb.$t.val();
            if (isNaN(cb.val)) {
                cb.$t.nextAll("select").html(cb.opt);
            } else {
                cb.$next = cb.$t.next('select').load(url + cb.val);
                if (!arrHiddenIDs) return;
                for (var j = 0; j < arrSelectIDs.length; j++) {
                    if (arrSelectIDs[j] == ('#' + cb.$t.attr("id"))) $(arrHiddenIDs[j]).val(cb.val);
                }
            }
        });
        if (!cb.sel.html()) cb.sel.html(cb.opt);
        
    }
}